package it.afs.wicket;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteSettings;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.model.util.ListModel;

public class StaticAutoCompleteTextField extends AutoCompleteTextField<String>
{
	private static final long serialVersionUID = -1815951717977897154L;
	
	private ListModel<String> sourceChoicesModel;
	
	public StaticAutoCompleteTextField(String id, List<String> sourceChoices)
	{
		super(id, new AutoCompleteSettings()
			.setAdjustInputWidth(true)
			.setMaxHeightInPx(550)
			.setShowListOnEmptyInput(false));
		
		this.sourceChoicesModel = new ListModel<String>(sourceChoices);
		this.setRequired(true);
	}

	@Override
	protected Iterator<String> getChoices(String input)
	{
		ArrayList<String> choices = new ArrayList<String>();
		for(String item : sourceChoicesModel.getObject())
		{
			if(item.toLowerCase().startsWith(input.toLowerCase()))
				choices.add(item);
		}
		return choices.iterator();
	}
	
	public List<String> getSourceChoices()
	{
		return sourceChoicesModel.getObject();
	}
	
	public void setSourceChoices(List<String> choices)
	{
		this.sourceChoicesModel = new ListModel<String>(choices);
	}
}
